﻿using System;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.VisualStudio.DebuggerVisualizers;

namespace OracleCommandVisualizer
{
    //Add ControlVisualizerObjectSource to solve serializable issue
    //REF: http://stackoverflow.com/questions/2959048
    public class OracleCommandVisualizerObjectSource : VisualizerObjectSource
    {
        public override void GetData(object target, Stream outgoingData)
        {
            XDocument xd = XDocument.Parse("<OracleCommand />");
            OracleCommand cmd = target as OracleCommand;
            xd.Root.Add(new XElement("CommandText", cmd == null ? 
                "null" : cmd.CommandText));
            xd.Root.Add(new XElement("Parameters"));
            XElement props = xd.Root.Element("Parameters");
            if (cmd != null)
                for (int i = 0; i < cmd.Parameters.Count; i++)
                {
                    OracleParameter p = cmd.Parameters[i];
                    props.Add(
                        new XElement(
                            "Parameter",
                            new XAttribute("No", i.ToString()),
                            new XAttribute("Name", p.ParameterName),
                            new XAttribute("DbType", p.DbType.ToString()),
                            new XAttribute("OracleType", p.OracleType.ToString()),
                            p.Value == null ? "null" : p.Value.ToString()
                            ));
                }
            base.GetData(xd.ToString(), outgoingData);
        }
    }
}
